<?
/**
 *会员类.
 *调用成员函数获取登录会员相关信息
 */
class Member_zax{
	/**
	 *会员ID
	 *@var int
	 */
	public $memberID;
	
	/**
	 *所属分销商ID,非普通会员与会员ID一致
	 *@var int
	 */
	public $fid;
	
	/**
	 *会员信息
	 *@var array
	 */
	public $m_info;
	
	/**
	 *票付通内部接口对象
	 *@var object
	 */
	public $s;
	
	/**
	 *构造函数.
	 *@param $s 票付通内部接口对象
	 *@int $memberID 会员ID
	 */
	public function __construct($s,$memberID){
		$this->fid=$this->memberID=$memberID;
		$str="select * from pft_member where id=".$memberID." limit 1";
		$GLOBALS['le']->query($str);
		$this->m_info=$GLOBALS['le']->fetch_assoc();
		$this->s=$s;
	}
	
	/**
	 *会员信息函数.
	 *@return array 返回会员信息键值数组
	 */
	public function info(){
		return $this->m_info;
	}
	
	/**
	 *查询常用联系人函数.
	 *@param int $sort 按键值顺序|逆序排列
	 *@return array 常用联系人键值数组，键联系人姓名，值联系人手机
	 */
	public function cinfos($sort=1){
		$str="select cinfos from pft_member_extinfo where fid=".$this->memberID;
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$cinfos=$GLOBALS['le']->f("cinfos");
		$out=array();
		if($cinfos){
			$cinfos=explode("|",$cinfos);
			if($sort==-1)
				krsort($cinfos);
			foreach($cinfos as $row){
				$info=explode(":",$row);
				$out[$info[0]]=$info[1];
			}
		}
		return $out;
	}
	
	/**
	 *添加常用联系人函数.
	 *@param string $ordername 联系人姓名
	 *@param string $ordertel 联系人手机
	 */
	public function addCinfo($ordername,$ordertel){
		
		if(!$ordername) return false;
		define("MAXNUM",9); //最多联系人数目
		$cinfos=$this->cinfos();
		$cname=array();
		$ctel=array();
		foreach($cinfos as $k=>$v){
			if($k){
				$cname[]=$k;
				$ctel[]=$v;
			}
		}
		$key=array_search($ordername,$cname);
		if($key===false){
			if(count($cname)>=MAXNUM){//如果数目大于做大允许数
				array_shift($cname);
				array_shift($ctel);
			}
			array_push($cname,$ordername);
			array_push($ctel,$ordertel);
		}else{
			$ctel[$key]=$ordertel;
			if($key!=(count($cname)-1)){//该联系人存在，位置移后
				$t=$cname[$key+1];
				$cname[$key+1]=$cname[$key];
				$cname[$key]=$t;
				$t=$ctel[$key+1];
				$ctel[$key+1]=$ctel[$key];
				$ctel[$key]=$t;
			}
		}
						
		$cinfos=array_combine($cname,$ctel);
		$out=array();
		
		foreach($cinfos as $k=>$v){
			$out[]=$k.":".$v;
		}
		$out=implode("|",$out);
		$str="update pft_member_extinfo set cinfos='$out' where  fid=".$this->memberID;
		$GLOBALS['le']->query($str);
	}
	
	/**
	 *查询会员账户余额函数.
	 *@return int 余额分单位
	 */
	public function money(){
		$fundXml=$this->s->PFT_Member_Fund($this->memberID);
		$fundXml=simplexml_load_string($fundXml);
		return (string)$fundXml->Rec->UUamoney;
	}
	
		
	/**
	 *查询会员账户可用授信额度函数.
	 *返回值是最大可欠额度加上已有额度
	 *@return int 额度分单位
	 */
	public function tmoney($aid){
		$fundXml=$this->s->PFT_Member_Fund($this->memberID,2,$aid);
		$fundXml=simplexml_load_string($fundXml);
		$basefund=$fundXml->Rec->UUbasecredit;
		$fundXml=$this->s->PFT_Member_Fund($this->memberID,1,$aid);
		$fundXml=simplexml_load_string($fundXml);
		$kfund=$fundXml->Rec->UUkmoney;
		$usefund=$basefund+$kfund;//可用购买额度
		return $usefund;
	}
	
	/**
	 *查询会员账户供应产品数函数.
	 *@return int
	 */
	public function pronum(){
		$str="select count(*) as num from uu_products where  p_status in (0,2,3,4,5) and apply_did=".$this->memberID;
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		return $GLOBALS['le']->f("num");
	}
	
	/**
	 *查询会员供应产品上架数函数.
	 *@return int
	 */
	public function upProNum(){
		$str="select count(*) as num from uu_products where apply_limit=1 and p_status in (0,2,3,4,5) and apply_did=".$this->memberID;
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		return $GLOBALS['le']->f("num");
	}
	
	/**
	 *查询会员供应产品下架数函数.
	 *@return int
	 */
	public function downProNum(){
		$str="select count(*) as num from uu_products where apply_limit=2 and p_status in (0,2,3,4,5) and apply_did=".$this->memberID;
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		return $GLOBALS['le']->f("num");
	}
	public function distributorNum(){
		$str="select count(*) as num from pft_member_relationship r join pft_member m on r.son_id=m.id where r.status=0 and m.status=0 and r.ship_type=0 and parent_id=".$this->memberID;
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		return $GLOBALS['le']->f("num");
	}
	
	/**
	 *查询会员合作分销商函数.
	 *@return array 会员ID数组
	 */
	public function distributors(){
		$str="select m.id from pft_member_relationship r join pft_member m on r.son_id=m.id where r.status=0 and m.status=0 and r.ship_type=0 and parent_id=".$this->memberID;
		$GLOBALS['le']->query($str);
		$r=array();
		while($row=$GLOBALS['le']->fetch_assoc()){
			$r[]=$row['id'];
		}
		return $r;
	}

	/**
	 *查询会员合作供应商函数.
	 *@return array 会员ID数组
	 */
	public function appiers(){
		$str="select m.id from pft_member_relationship r join pft_member m on r.parent_id=m.id where r.status=0 and m.status=0 and r.ship_type=0 and son_id=".$this->memberID;
		$GLOBALS['le']->query($str);
		$r=array();
		while($row=$GLOBALS['le']->fetch_assoc()){
			$r[]=$row['id'];
		}
		return $r;
	}
	
	/**
	 *查询已配置账单设置分销商.
	 *@return array 会员ID数组
	 */
	public function settlefids(){
		$str="select fid from uu_settle where aid=".$this->memberID;
		$GLOBALS['le']->query($str);
		$r=array();
		while($row=$GLOBALS['le']->fetch_assoc()){
			$r[]=$row['fid'];
		}
		return $r;
	}
	
	/**
	 *查询已配置账单设置供应商.
	 *@return array 会员ID数组
	 */
	public function settleaids(){
		$str="select aid from uu_settle where fid=".$this->memberID;
		$GLOBALS['le']->query($str);
		$r=array();
		while($row=$GLOBALS['le']->fetch_assoc()){
			$r[]=$row['aid'];
		}
		return $r;
	}
	
	/**
	 *查询会员供应产品数.
	 *@return int 
	 */
	public function supplierNum(){
		$str="select count(*) as num from pft_member_relationship where status=0 and ship_type=0 and son_id=".$this->memberID;
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		return $GLOBALS['le']->f("num");
	}
	
	/**
	 *查询会员可分销产品信息.
	 *@param $lid 景区id，默认不传，传入只返回改景区产品
	 *@return array 会员可分销产品，含直接供应和转供应产品
	 */
		public function salePros($lid=Null,$pageSize=null,$offset=null){
		$r=array();
		$salePids=array();
		$str="select * from uu_priceset where pid=".$this->fid;
		$GLOBALS['le']->query($str);
		while($row=$GLOBALS['le']->fetch_assoc()){
			$dprices[$row['tid']."_".$row['aid']]=$row['dprice'];
		}
		$sql="select p.id as pid,p.apply_did,p.p_status,l.id as lid,l.p_type,"
            ."l.title,l.area,l.topic,l.px,l.runtime,l.taobao_support,l.imgpath,"
            ."l.address,t.id as tid,t.pay,t.getaddr,t.title as ttitle,t.tprice,t.notes,"
            ."t.buy_limit_low,t.ddays,t.delaydays,t.status,f.ass_station,f.mdays,f.mhour,f.rdays"
            ." from uu_products p left join uu_land l on p.contact_id=l.id left join
			uu_jq_ticket t on p.id=t.pid left join uu_land_f f on f.pid=p.id";
		$where="where p.p_status in (0,3,4,5) and p.apply_limit=1";
		if($lid) $where.=" and l.id=$lid ";
		$orderby=" order by l.px desc ";
			
		/*一手供应商产品*/
		$aids=array(0=>$this->fid);
		$str="select l.pids,l.aid from pft_product_sale_list l join pft_member m on l.aid=m.id where l.fid=".$this->fid." and l.status=0 and m.status=0";
		$GLOBALS['le']->query($str);
		while($row=$GLOBALS['le']->fetch_assoc()){
			$salePids[$row['aid']]=explode(",",$row['pids']);
			$aids[]=$row['aid'];
		}        
        //原方法:先从pft_product_sale_list里取出所有上级供应商 再联合uu_land表去找景区 跳回最开始的sql语句
        /*取总数先 把一手和二手的总数分别算出来          
        *当偏移量小于一手总数时 只取一手里 
        *当取到一手最后几个产品时 再从二手里补几个 
        *当取的全都在二手里时 你懂的
        *by KaZeline zax 2015-01-15
        */
        //二手供应商产品 数量统计 下面的搬上来了
        $str2="select * from pft_p_apply_evolute where fid=".$this->fid." and status=0 and sid<>sourceid";
		$GLOBALS['le']->query($str2);
		while($row=$GLOBALS['le']->fetch_assoc()){
			$secondInfo[$row['pid']."_".$row['sid']]=$row;
			$secondPids[]=$row['pid'];
            
		}
        // print_r($secondPids);
        // exit;
        
        //看看要选择的景点们在哪个位置
        if($currentPage && $offset){
            $sel_total1 = 'select count(*) as total1 '
                                .' from uu_products p left join uu_land l on p.contact_id=l.id left join
                                    uu_jq_ticket t on p.id=t.pid left join uu_land_f f on f.pid=p.id '
                                .$where." and p.apply_did in ($aids)";
            $sel_total2 = 'select count(*) as total2 
                                from uu_products p left join uu_land l on p.contact_id=l.id left join
                                    uu_jq_ticket t on p.id=t.pid left join uu_land_f f on f.pid=p.id 
                                where tid in ('.implode(',',$secondPids).')'
                                .$where." and p.id in ($secondPids) ";
            
            $GLOBALS['le']->query($sel_total1);
            $GLOBALS['le']->fetch_assoc();
            $total1 = $le->f('total1');
            
            $GLOBALS['le']->query($sel_total2);
            $GLOBALS['le']->fetch_assoc();
            $total2 = $le->f('total2');
            
            $total = $pageSize + $offset;
            if($total1 >= $total ){
                $pageSize1 = $pageSize;
                $offset1 = $offset;
            }else if($total1 < $total && $total1 >= $offset){
                $pageSize1 = $total1 - $offset;
                $pageSize2 = $total - $total1;
                $offset1 = $offset;
                $offset2 = '0';
            }elseif($total1<$offset){
                $offset2 = $total1;
                $pageSize2 = $pageSize;
            }
        }
        var_dump($offset1) ;
        echo "<hr>";
        echo $pageSize1 ;
        echo "<hr>";
        echo $offset2 ;
        echo "<hr>";
        echo $pageSize2 ;
        echo "<hr>";
        // exit;
        if(($pageSize1!==null && $offset1!==null) || (!$currentPage && !$offset)){
        
            if(count($aids)!=0){
                $aids=implode(",",$aids);
                $where1=$where." and p.apply_did in ($aids) ";
                $str1="$sql $where1 $orderby";
                if($pageSize1!==null && $offset1!==null)
                    $str1 .= "limit $offset1,$pageSize1";
                $GLOBALS['le']->query($str1);
                while($row=$GLOBALS['le']->fetch_assoc()){
                    if($row['apply_did']!=$this->fid&&$salePids[$row['apply_did']][0]!="A"&&!in_array($row['pid'],$salePids[$row['apply_did']])) continue;
                    
                    if(($row['jsprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1))==-1){
                        $row['jsprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2);
                    }
                    if(($row['uprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1,"","",1))==-1){
                        $row['uprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2,"","",1);
                    }
                    if($row['jsprice']==-1||$row['uprice']==-1) continue;
                    $row['jsprice']=$row['jsprice']+$dprices[$row['pid']."_0"];
                
                    $row['sapply_did']=$row['apply_did'];
                    $row['aid']=0;
                    $row['lvl']=0;
                    $r[]=$row;
                }
            }
        
        }
		
		/*二手供应商产品*/
        //搬上去面了
		// $str="select * from pft_p_apply_evolute where fid=".$this->fid." and status=0 and sid<>sourceid";
		// $GLOBALS['le']->query($str);
		// while($row=$GLOBALS['le']->fetch_assoc()){
			// $secondInfo[$row['pid']."_".$row['sid']]=$row;
			// $secondPids[]=$row['pid'];
		// }
        if(($pageSize2!==null && $offset2!==null) || (!$currentPage && !$offset)){
        
            if(count($secondPids)!=0){
                $dprices2=array();
                $str="select * from uu_priceset where tid in (".implode(",",$secondPids).")";
                $GLOBALS['le']->query($str);
                while($row=$GLOBALS['le']->fetch_assoc()){
                    $dprices2[$row['tid']."_".$row['pid']."_".$row['aid']]=$row['dprice'];
                }
                $proInfo=array();
                $secondPids=implode(",",$secondPids);
                $where2=$where." and p.id in ($secondPids) ";
                $str2="$sql $where2 $orderby";
                 if($pageSize2!==null && $offset2!==null)
                    $str2 .= "limit $offset2,$pageSize2";
                $GLOBALS['le']->query($str2);
                while($row=$GLOBALS['le']->fetch_assoc()){
                    if(($row['jsprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1))==-1){
                        $row['jsprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2);
                    }
                    if(($row['uprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1,"","",1))==-1){
                        $row['uprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2,"","",1);
                    }
                    if($row['jsprice']==-1||$row['uprice']==-1) continue;
                    $proInfo[$row['pid']]=$row;
                }
                foreach($secondInfo as $key=>$value){
                    $key=explode("_",$key);
                    $pid=$key[0];
                    $sapply_did=$key[1];
                    if(!$proInfo[$pid]) continue;
                    $row=$proInfo[$pid];
                    $row['sapply_did']=$sapply_did;
                    $row['aid']=$sapply_did;
                    $row['lvl']=$value['lvl'];
                    $row['eid']=$value['id'];
                    $row['estatus']=$value['status'];
                    
                    $aids=explode(",",$value['aids']);
            
                    $aids[]=$value['fid'];
                    $count=count($aids);
                    $aids[0]=0;
                    for($i=1;$i<$count;$i++){
                        $row['jsprice']+=$dprices2[$row['pid']."_".$aids[$i]."_".$aids[$i-1]];
                    }
                    $r[]=$row;
                }
            }
        }
		return $r;
	}
}

/**
 *普通会员类.
 *可分销产品以传入分销商ID为准，分销价按零售价计算
 */
// class GuestMember extends Member{
	// public function __construct($s,$memberID,$fid){
		// parent::__construct($s,$memberID);
		// $this->fid=$fid;
	// }
// }
?>